﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.Networking;

public class GameLoader : MonoBehaviour
{
    public string ipOrDomain = "127.0.0.1";
    public int port = 6555;

    public int curUser = 1;

    // Start is called before the first frame update
    void Start()
    {
        string ipStr = ipOrDomain;
        if (string.IsNullOrEmpty(ipOrDomain))
        {
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[ipHostInfo.AddressList.Length - 1];
            ipStr = ipAddress.ToString();
        }
        MGame.instance.InitSocket(ipStr, port, OnConnected, OnConnectFail);
    }

    private void OnConnected(string arg1, int arg2)
    {
        GameDebug.Log($"OnConnected:{arg1}  arg2:{arg2}");


        RequestServerController.instance.ReqLogin(curUser,() =>
        {
            //MGame.instance.GetUserMoments(()=>
            {
                LoadingSceneController.LoadScene("Main");
            }
            //);
        }, Start);

        //Request_Login requestBase = new Request_Login();
        //requestBase.loginReqData = Request_Login.GetDefaultData();// new LoginRequestData();
        //UserInfo userInfo = MGame.instance.MUser.userInfo;

        //bool needRetIcon = Tex.IsNull(userInfo.icon);
        //bool needRetTex1 = Tex.IsNull(userInfo.tex1);
        //bool needRetTex2 = Tex.IsNull(userInfo.tex2);
        //bool needRetTex3 = Tex.IsNull(userInfo.tex3);
        //bool needRetTex4 = Tex.IsNull(userInfo.tex4);
        //bool needRetTex5 = Tex.IsNull(userInfo.tex5);
        //bool needRetTex6 = Tex.IsNull(userInfo.tex6);
        //requestBase.loginReqData.needReturnIcon = needRetIcon;
        //requestBase.loginReqData.needReturnTex1 = needRetTex1;
        //requestBase.loginReqData.needReturnTex2 = needRetTex2;
        //requestBase.loginReqData.needReturnTex3 = needRetTex3;
        //requestBase.loginReqData.needReturnTex4 = needRetTex4;
        //requestBase.loginReqData.needReturnTex5 = needRetTex5;
        //requestBase.loginReqData.needReturnTex6 = needRetTex6;

        //GameDebug.Log($"needRetIcon:{needRetIcon}  needRetTex1:{needRetTex1} needRetTex2:{needRetTex2} needRetTex3:{needRetTex3}");

        //if (string.IsNullOrWhiteSpace(GameSettingViewController.username))
        //{
        //    requestBase.loginReqData.uuid = GetUUID();
        //}
        //else
        //{
        //    requestBase.loginReqData.uuid = GameSettingViewController.username;
        //    requestBase.loginReqData.password = GameSettingViewController.password;
        //}
        //requestBase.loginReqData.isRobot = false;

        //requestBase.onResponse = delegate (ResponseBase response)
        //{
        //    Response_Login login = response as Response_Login;

        //    GameDebug.Log($"Request_Login onResponse :{login.user.userInfo}");

        //    MGame.instance.MUser.InitWithServerData(login.user,needRetIcon,needRetTex1,needRetTex2,needRetTex3,
        //        needRetTex4,needRetTex5,needRetTex6);

        //    LoadingSceneController.LoadScene("Main");
        //};
        //requestBase.onTimeOut = delegate (RequestBase req)
        //{
        //    GameDebug.Log("Request_Login onTimeOut ");
        //    //Game.Instance.ShowNoticePanel(Localization.instance.GetValue("login_request_timed_out"));
        //    //onFinish(Localization.instance.GetValue("login_request_timed_out"), null);
        //};

        //requestBase.clientSocket = MGame.instance.clientSocket;
        //requestBase.timeOutSeconds = 12;
        //requestBase.Send();
    }
   
    private void OnConnectFail()
    {
        GameDebug.LogError($"OnConnectFail");
        MGame.instance.ShowNoticePanel("请检查网络", () =>
        {
            MGame.instance.clientSocket.BeginConnect(ipOrDomain, port);
        });
        //LoadingSceneController.LoadScene("Main");
    }

//    public static bool EditorTest;
//    private const string GUIDKey = "KEY_GUID";
//    string guid
//    {
//        get
//        {
//            if (PlayerPrefs.HasKey(GUIDKey))
//            {
//                return PlayerPrefs.GetString(GUIDKey);
//            }
//            else
//            {
//                string guid = Guid.NewGuid().ToString()
//                    .Replace("-", "");

//                PlayerPrefs.SetString(GUIDKey, guid);

//                return guid;
//            }
//        }
//    }

//    public string GetUUID()
//    {
//#if UNITY_EDITOR
//        if (EditorTest)
//            return GetDeviceId();
//        else
//            return guid;
//#endif

//        return GetDeviceId();
//    }

//    public static string GetDeviceId()
//    {
//        bool is_iOS = UnityEngine.Application.platform == UnityEngine.RuntimePlatform.IPhonePlayer;

//        string id = UnityEngine.SystemInfo.deviceUniqueIdentifier;
//#if UNITY_IOS
//        id = is_iOS ? UnityEngine.iOS.Device.advertisingIdentifier : id; // iOS 目前使锟矫癸拷锟斤拷识锟斤拷
//#endif
//        return id;
//    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
